﻿String.prototype.trim = function()
{
  return this.replace(/^\s+/g,"").replace(/\s+$/g,"");
}

$(document).ready(function(){

//    $("#AServiceItem").click(function(){
//        url="ServiceItem.htm";
//        window.open(url,'regconfirm','height=620,width=850,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');
//        return true;
//    });
    /////////////////////////////////////////////////////////////////
    //第一步
    //用户Email获得焦点事件
    $("#txtUserName").focus(function(){
        $("#divUserName").html("登录帐号由5-20个英文字母或数字组成，一旦注册成功不得更改。");
        $("#divUserName").css("color","#858585");
    });
    //Email失去焦点事件
    $("#txtUserName").blur(function(){
    CheckUserName();
    });
    
    //密码框获得焦点

    $("#txtUserPwd").focus(function(){
        $("#divUserPwd").html("登录密码由6-20个英文字母或数字组成。");
        $("#divUserPwd").css("color","#858585");
    });
    //密码框失去焦点
    $("#txtUserPwd").blur(function(){
        var ret=0;
        var Cret=0;
        ret=chkPassword();
        Cret=chkPassword2();
        if(ret==0)
        {
            $("#divUserPwd").html("<img src=\"Images/RegError.png\" />请输入您的账号密码！");
            $("#divUserPwd").css("color","#CD4C09");
        }
        else if(ret==-1)
        {
            $("#divUserPwd").html("<img src=\"Images/RegError.png\" />密码格式有误，请输入正确格式的密码，6-15个字符");
            $("#divUserPwd").css("color","#CD4C09");
        }
        else if(ret==1)
        {
            $("#divUserPwd").html("<img src=\"Images/RegRight.png\" />");
            $("#divUserPwd").css("color","#9BC865");
        }
        if(Cret!=0)
        {
            if(ret!=1)
            {
                $("#divUserPwdRP").html("<img src=\"Images/RegError.png\" />请先确认上面密码的有效性！");
                $("#divUserPwdRP").css("color","#CD4C09");
            }
            else
            {
                if(Cret==-1)
                {   
                    $("#divUserPwdRP").html("<img src=\"Images/RegError.png\" />两次输入的密码不一致！");
                    $("#divUserPwdRP").css("color","#CD4C09");
                }
                if(Cret==1)
                {
                    $("#divUserPwdRP").html("<img src=\"Images/RegRight.png\" />");
                    $("#divUserPwdRP").css("color","#9BC865");
                }
            }
        }

    });
    //密码确认框获得焦点
     $("#txtUserPwdRP").focus(function(){
        $("#divUserPwdRP").html("请确认你上面输入的密码，以确保您的密码的有效性！");
        $("#divUserPwdRP").css("color","#858585");
     });
      //密码确认框失去焦点
     $("#txtUserPwdRP").blur(function(){
        if(chkPassword()==1)
        {
            var CRet=0;
            CRet=chkPassword2();
            if(CRet==0)
            {
                $("#divUserPwdRP").html("<img src=\"Images/RegError.png\" />请再次确认您输入的账号密码！");
                $("#divUserPwdRP").css("color","#CD4C09");
            }
            else if(CRet==-1)
            {
                $("#divUserPwdRP").html("<img src=\"Images/RegError.png\" />两次输入的密码不一致！");
                $("#divUserPwdRP").css("color","#CD4C09");
            }
            else if(CRet==1)
            {
                $("#divUserPwdRP").html("<img src=\"Images/RegRight.png\" />");
                $("#divUserPwdRP").css("color","#9BC865");
            }
        }
        else
        {
            $("#divUserPwdRP").html("<img src=\"Images/RegError.png\" />请先保证您上次输入密码的有效性！");
            $("#divUserPwdRP").css("color","#CD4C09");
        }
     });
         //手机输入框获得焦点
     
    
     //姓名输入框获得焦点
        $("#txtCompanyName").focus(function(){
        $("#divCompanyName").html("请填写您的企业名称以方便我们的沟通！");
        $("#divCompanyName").css("color","#858585");
     });
     
     $("#txtCompanyName").blur(function(){
     if(jQuery.trim($("#txtCompanyName").val())=="")
     {
        $("#divCompanyName").html("<img src=\"Images/RegError.png\" />请输入企业名称！");
        $("#divCompanyName").css("color","#CD4C09");
     }
     else
     {
         $("#divCompanyName").html("<img src=\"Images/RegRight.png\" />");
         $("#divCompanyName").css("color","#9BC865");
     }
     });
     
    //法人名称输入框获得焦点
        $("#txtLegalName").focus(function(){
        $("#divLegalName").html("请填写企业法人以方便联系！");
        $("#divLegalName").css("color","#858585");
     });
     
     $("#txtLegalName").blur(function(){
     if(jQuery.trim($("#txtLegalName").val())=="")
     {
        $("#divLegalName").html("<img src=\"Images/RegError.png\" />请填写企业法人以方便联系！");
        $("#divLegalName").css("color","#CD4C09");
        
     }
     else
     {
        $("#divLegalName").html("<img src=\"Images/RegRight.png\" />");
        $("#divLegalName").css("color","#9BC865");
     }
     });
     
     
     //手机输入框获得焦点
     $("#txtUserPhone").focus(function(){
        $("#divUserPhone").html("请正确填写您的联系电话以方便我们及时帮助您！");
        $("#divUserPhone").css("color","#858585");
     });
     
     $("#txtUserPhone").blur(function(){
         var Mret=0;
         Mret=chkMobile();
         if(Mret==0)
         {
            $("#divUserPhone").html("<img src=\"Images/RegError.png\" />请填写您的手机或座机以方便我们联系您！");
            $("#divUserPhone").css("color","#CD4C09");
         }
         else if(Mret==-1)
         {
            $("#divUserPhone").html("<img src=\"Images/RegError.png\" />您输入的联系方式无效！");
            $("#divUserPhone").css("color","#CD4C09");
         }
         else if(Mret==1)
         {
            $("#divUserPhone").html("<img src=\"Images/RegRight.png\" />");
            $("#divUserPhone").css("color","#9BC865");
         }
    });
    
    $("#txtMobilePhone").focus(function(){
        $("#divMobilePhone").html("请正确填写您的手机号码以方便及时帮助您！");
        $("#divMobilePhone").css("color","#858585");
     });
     
     $("#txtMobilePhone").blur(function(){
         var Mret=0;
         Mret=chkMobileP();
         if(Mret==0)
         {
            $("#divMobilePhone").html("<img src=\"Images/RegError.png\" />请填写您的手机号码以方便我们联系您！");
            $("#divMobilePhone").css("color","#CD4C09");
         }
         else if(Mret==-1)
         {
            $("#divMobilePhone").html("<img src=\"Images/RegError.png\" />您输入的联系方式无效！");
            $("#divMobilePhone").css("color","#CD4C09");
         }
         else if(Mret==1)
         {
            $("#divMobilePhone").html("<img src=\"Images/RegRight.png\" />");
            $("#divMobilePhone").css("color","#9BC865");
         }
    });
        //联系地址输入框获得焦点
        $("#txtRegAddress").focus(function(){
        $("#divRegAddress").html("请填写您的联系地址！");
        $("#divRegAddress").css("color","#858585");
     });
     
     $("#txtRegAddress").blur(function(){
     if(jQuery.trim($("#txtRegAddress").val())=="")
     {
        $("#divRegAddress").html("<img src=\"Images/RegError.png\" />请输入您的地址！");
        $("#divRegAddress").css("color","#CD4C09");
        
     }
     else
     {
         $("#divRegAddress").html("<img src=\"Images/RegRight.png\" />");
         $("#divRegAddress").css("color","#9BC865");
     }
     });
     
             //联系地址输入框获得焦点
        $("#txtOffAddress").focus(function(){
        $("#divOffAddress").html("请填写您的联系地址！");
        $("#divOffAddress").css("color","#858585");
     });
     
     $("#txtOffAddress").blur(function(){
     if(jQuery.trim($("#txtOffAddress").val())=="")
     {
        $("#divOffAddress").html("<img src=\"Images/RegError.png\" />请输入您的地址！");
        $("#divOffAddress").css("color","#CD4C09");
        
     }
     else
     {
         $("#divOffAddress").html("<img src=\"Images/RegRight.png\" />");
         $("#divOffAddress").css("color","#9BC865");
     }
     });
     
    //联系地址输入框获得焦点
        $("#txtValCode").focus(function(){
        $("#divValCode").html("请填写图片验证码！");
        $("#divValCode").css("color","#858585");
     });
     
     $("#txtValCode").blur(function(){
     var Mret=0;
         Mret=chkValCode();
         if(Mret==0)
         {
          $("#divValCode").html("<img src=\"Images/RegError.png\" />请输入图片验证码！");
        $("#divValCode").css("color","#CD4C09");
         }
         else if(Mret==-1)
         {
          $("#divValCode").html("<img src=\"Images/RegError.png\" />您输入的图片验证码格式有误！");
        $("#divValCode").css("color","#CD4C09");
         }
         else if(Mret==1)
         {
          $("#divValCode").html("");
        $("#divValCode").css("color","#9BC865");
         }
         
     });
     
    $("#btnSubmit").click(function(){
        if(chkOneStep()==true)
        {
        $(this).disable= true;
            var Mt=RegUser();
            if(Mt!="1")
            {
                alert(Mt);
                
            }
            else
            {
                alert("注册成功，请登陆！")
                location.href="Login.aspx";
            }
        $(this).disable= false;
        }
    });
    
});

function IsInteger(objval)
{
     var reg=/^[0-9]+$/;
    if(reg.test(objval))
    {
        return true;
    }
    else
    {
        return false;
    }
}
 function regMobile(objval)
    {
        var reg=/^(13[0-9]|15[0-9]|18[0-9])\d{8}$/;
        if(reg.test(objval))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    //检查固定电话
    function chkLinkPhone(objval)
    {
        var reg=/^\(0\d{2}\)[- ]?\d{8}$|^0\d{2}[- ]?\d{8}$|^\(0\d{3}\)[- ]?\d{7}$|^0\d{3}[- ]?\d{7}$|(13[0-9]|15[0-9]|18[0-9])\d{8}$/;
        if(reg.test(objval))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    function chkEmail(objval)
    {
        var reg=/^[\w-]+@[\w-]+\.(com|net|org|edu|mil|tv|biz|info)(\.cn)?$/;
        if(reg.test(objval))
        {
            return true;
        }
        else
        {
            return false;
        } 
    }
    function chkPostCode(objval)
    {
        var reg=/^[1-9]\d{5}$/;
        if(reg.test(objval))
        {
            return true;
        }
        else
        {
            return false;
        } 
    }
    function chkMobile()
    {
        if($("#txtUserPhone").val().trim().length==0)
        return 0;
        if(chkLinkPhone($("#txtUserPhone").val()))
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }
        function chkMobileP()
    {
        if($("#txtMobilePhone").val().trim().length==0)
        return 0;
        if(regMobile($("#txtMobilePhone").val()))
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }
    function chkValCode()
    {
        if($("#txtValCode").val().trim().length==0)
        return 0;
        if(IsInteger($("#txtMobilePhone").val()))
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }
    function chkRegValid()
    {
        var RegName = $("#txtUserName").val().trim();
        var Ret = 0 ;
        if(RegName.length>=5 && RegName.length<=20)
        {
             $.ajax({
            url: "Ashx/RegValid.ashx",
            type: "post",
            async:false,
            data: {UserName:RegName},
            dataType:"json",
            success: function(json) {
                var Data = json.XGReg;
                $.each(Data, function(i, n) 
                {
                    if(n.sta=="hadreg")
                    {
                        Ret=2;
                    }
                    else if(n.sta=="fail")
                    {
                        Ret=3;
                    }
                    else if(n.sta=="pass")
                    {
                        Ret=1;
                    }
                });
            }
        });
        return Ret;
        }
        else
        {
            return Ret;
        }
    }
function CheckUserName()
{
    var Eret = 0;
    Eret = chkRegValid();
    var lvMsg="请输入您要注册的用户名！";
    if(Eret!=1)
    {
        if(Eret==0 || Eret==3)
        {
            lvMsg="请填写正确格式用户名,长度为5-20！";
        }
        else if(Eret==2)
        {
            lvMsg="已存在此账户，请尝试其他用户名！";
        }
       
        $("#divUserName").html("<img src=\"Images/RegError.png\" />"+lvMsg);
        $("#divUserName").css("color","#CD4C09");
    }
    else
    {
        $("#divUserName").html("<img src=\"Images/RegRight.png\" />");
        $("#divUserName").css("color","#9BC865");
    }
}
//检查一次密码
function chkPassword(){
	password= $("#txtUserPwd").val().trim();
	if(password.length==0) return 0;
	var len;
	var i;
	var isPassword = true;
	len = 0;
	for (i=0;i<password.length;i++){
		if (password.charCodeAt(i)>254) isPassword = false;
	}
	if(!isPassword || password.length >= 20 || password.length < 6)
		return -1;
	return 1;
}
//检查二次密码
function chkPassword2(){
    Cpassword=$("#txtUserPwdRP").val().trim();
    if(Cpassword.length==0) return 0;
    if(Cpassword!=$("#txtUserPwd").val().trim())
    {
        return -1;
    }
    else
    {
        return 1;
    }
}


function chkOneStep()
{
   
    if(chkRegValid()!=1)
    {
        CheckUserName();
        return false;
    }
    
    if(chkPassword()!=1)
    {
        $("#txtUserPwd").blur();
        return false;
    }
    if(chkPassword2()!=1)
    {
        $("#txtUserPwdRP").blur();
        return false;
    }

    if(jQuery.trim($("#txtCompanyName").val())=="")
    {
        $("#txtCompanyName").blur();
        return false;
    }
    if(jQuery.trim($("#txtLegalName").val())=="")
    {
        $("#txtLegalName").blur();
        return false;
    }
    if(chkMobile()!=1)
    {
        $("#txtUserPhone").blur();
        return false;
    }
    if(chkMobileP()!=1)
    {
        $("#txtMobilePhone").blur();
        return false;
    }
    if(jQuery.trim($("#txtRegAddress").val())=="")
    {
        $("#txtRegAddress").blur();
        return false;
    }
        if(jQuery.trim($("#txtOffAddress").val())=="")
    {
        $("#txtOffAddress").blur();
        return false;
    }

    return  true;
}

function RegUser()
{
    var RCret="0";
    var username = $("#txtUserName").val().trim(); 
    var password = $("#txtUserPwdRP").val().trim(); 
    var companyname =  $("#txtCompanyName").val().trim(); 
    var legalname =  $("#txtLegalName").val().trim(); 
    var telephone=$("#txtUserPhone").val().trim();
    var mobilephone=  $("#txtMobilePhone").val().trim(); 
    var regaddress =  $("#txtRegAddress").val().trim(); 
    var offaddress =  $("#txtOffAddress").val().trim(); 
    var valcode = $("#txtValCode").val().trim(); 
    
     $.ajax({
            url: "Ashx/UserRegister.ashx",
            type: "post",
            async:false,
            data: {
                UserName: username,
                Password: password,
                CompanyName:companyname,
                LegalName:legalname,
                TelePhone: telephone,
                MobilePhone:mobilephone,
                RegAddress:regaddress,
                OffAddress:offaddress,
                ValCode:valcode
            },
            dataType: "json",
            success: function(json) {
                var Data = json.XGReg;
                $.each(Data, function(i, n) {
                     if(n.sta=="fail" || n.sta=="hadreg")
                    {
                        RCret=n.msg;
                    }
                    if(n.sta=="pass")
                    {
                        RCret="1";
                    }
                });
            }
        });
      return RCret;
}